package com.hxd.charitysystem.adapter.CardItemAdapter;


import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.hxd.charitysystem.R;
import com.hxd.charitysystem.bean.HomeCardItemListBean.CardItem;
import com.hxd.charitysystem.image.ImageLoader;
import com.hxd.charitysystem.utils.ProjectUrl;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名：    CharitySystem
 * 包名：      com.hxd.charitysystem.item_card_home.CardItemAdapter
 * 文件名:     CardPagerAdapter
 * 创建者:     HXD
 * 创建时间:   2019/4/29 10:22
 * 描述:       TODO
 */


public class CardPagerAdapter extends PagerAdapter implements HomeCardAdapter {

    private List<CardView> mViews;
    private List<CardItem> mData;
    private float mBaseElevation;

    private CardItemClick listener;

    public interface CardItemClick{
        void cardItemClick(String data);
    }

    public void setOnCardItemClickListener(CardItemClick listener){
        this.listener = listener;
    }

    public CardPagerAdapter() {
        mData = new ArrayList<>();
        mViews = new ArrayList<>();
    }

    public void addCardItem(CardItem item) {
        mViews.add(null);
        mData.add(item);
    }

    public void setInitData(){
        mData = new ArrayList<>();
        mViews = new ArrayList<>();
    }


    public float getBaseElevation() {
        return mBaseElevation;
    }

    @Override
    public CardView getCardViewAt(int position) {
        return mViews.get(position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext())
                .inflate(R.layout.item_card_home, container, false);
        container.addView(view);
        bind(mData.get(position), view);
        CardView cardView = (CardView) view.findViewById(R.id.cardView);

        if (mBaseElevation == 0) {
            mBaseElevation = cardView.getCardElevation();
        }

        cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
        mViews.set(position, cardView);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        mViews.set(position, null);
    }

    private void bind(final CardItem item, View view) {
        CardView cardItem = view.findViewById(R.id.cardView);
        ImageView helpPhoto = view.findViewById(R.id.iv_photo_card_home);
        ImageView fundraisingPhoto = view.findViewById(R.id.iv_fundraisingPhoto_card_home);
        TextView fundraisingTitle = view.findViewById(R.id.tv_title_card_home);
        TextView fundraisingName = view.findViewById(R.id.tv_fundraisingName_card_home);
        TextView fundraisingType = view.findViewById(R.id.tv_fundraisingType_card_home);
        TextView fundraisingAddress = view.findViewById(R.id.tv_fundraisingAddress_card_home);
        TextView fundraisingMoney = view.findViewById(R.id.tv_fundraisingMoney_card_home);
        TextView helpCount = view.findViewById(R.id.tv_helpCount_card_home);
        ProgressBar account = view.findViewById(R.id.pb_account_card_home);
        if (item.getAssistantImages() != null){
            //获取第一张附图当主图
            ImageLoader.loadImage(helpPhoto, ProjectUrl.getUserImage + item.getAssistantImages().get(0));
        }
        if(item.getUserHeader() != null){
            ImageLoader.loadCircleImage(fundraisingPhoto, ProjectUrl.getUserImage + item.getUserHeader());
        }
        fundraisingTitle.setText(item.getTitle());
        fundraisingName.setText(item.getUsername());
        fundraisingType.setText(item.getType());
        fundraisingAddress.setText(item.getAddress());
        fundraisingMoney.setText(item.getRaisedAmount() + "元");
        helpCount.setText(item.getHelpCount() + "次");
        account.setProgress((int)((item.getRaisedAmount() / item.getTargetAmount()) * 100));

        cardItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.cardItemClick(item.CardItemToJson());
            }
        });
    }

}
